//
//  RecommendMerchantDetailVC.m
//  ImGuider
//
//  Created by 王鹏宇 on 2018/6/5.
//  Copyright © 2018年 imguider. All rights reserved.
//

#import "RecommendMerchantDetailVC.h"
#import "RecommendMerchantCell.h"
#import "MerchantIntroduce.h"
#import "MerchantDescCell.h"
#import "MerchantInforCell.h"
#import "SectionTitleHeaderView.h"
#import "RecommendMerchantDetailVC.h"
#import "ScenicDetailIntroduce.h"
#import "MerchantEventCell.h"
#import "WebViewVC.h"
#import "GLHBannerView.h"
@interface RecommendMerchantDetailVC ()<ScenicDetailIntroduceDelegate>

@property (strong, nonatomic) GLHBannerView *headScrollView;

@property (nonatomic, strong)NSMutableArray * tagArray;

@end

@implementation RecommendMerchantDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setup];
    [self loadData];
}


-(void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    
    if ([self.model.playpath isEqualToString:[IGAvPlayerManager shareManeger].currentURL]) {
         [[IGAvPlayerManager shareManeger] stop];
    }
}

- (void)setup {
    
//    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenWidth /16 *9) ];
    
    self.headScrollView = [[GLHBannerView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenWidth * 9 / 16)];
    
//    _recommendImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenWidth /16 *9)];
//    _recommendImageView.image = [UIImage imageNamed:@"placeholder_rec"];
//    [headView addSubview:_recommendImageView];
//
//    UIView *mengbanView = [UIView new];
//    mengbanView.backgroundColor = kMengBanColor;
//    [self.recommendImageView addSubview:mengbanView];
//    [mengbanView mas_makeConstraints:^(MASConstraintMaker *make) {
//        
//        make.edges.equalTo(self.recommendImageView);
//    }];
    
    self.tableView.tableHeaderView = self.headScrollView;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self closeAutomaticallyAdjustsScrollViewInsets:self.tableView];
    
    self.barBGAlpha = 0;
    self.customBarStyle = UIStatusBarStyleLightContent;
    [self setAutoRowHeight:self.tableView];
    
    [self registCells:@[@"RecommendMerchantCell",@"MerchantIntroduce",@"MerchantDescCell",@"MerchantInforCell",@"ScenicDetailIntroduce",@"MerchantEventCell"]];
    
    [self.tableView registerClass:[SectionTitleHeaderView class] forHeaderFooterViewReuseIdentifier:@"SectionTitleHeaderView"];
    self.tableView.backgroundColor = kBGColor;
}


- (void)loadData {
    
    @weakify(self);
    [NetAPIManager getBusinessDetailID:self.model.mid type:self.model.type block:^(MerchantModel *model, NSError *error) {
        @strongify(self);
        self.model = model;
        [self setupTagArray];
    }];
    
}


- (void)setupTagArray {
    
//    [self.recommendImageView setImageURL:[NSURL URLWithString:self.model.pictureArray.firstObject] placeholderImage:kPlaceholderRec];
    
    [self.headScrollView setBannerImageArray:self.model.pictureArray];
    
    self.tagArray = [NSMutableArray arrayWithArray:@[@(MerchantTypeAdress),@(MerchantTypeOpenTime),@(MerchantTypeTel),@(MerchantTypeWeb)]];
    
//    if (self.model.smartName.length == 0) {
//        [self.tagArray removeObject:@(MerchantTypeName)];
//    }
//
//    if (self.model.smartIntro.length == 0) {
//        [self.tagArray removeObject:@(MerchantTypeDesc)];
//    }
    
    if (self.model.address.length == 0) {
        [self.tagArray removeObject:@(MerchantTypeAdress)];
    }
    
    if (self.model.opentime.length == 0) {
        [self.tagArray removeObject:@(MerchantTypeOpenTime)];
    }

    if (self.model.tel.length == 0) {
        
        [_tagArray removeObject:@(MerchantTypeTel)];
    }
    
    if (self.model.website.length == 0) {
        [_tagArray removeObject:@(MerchantTypeWeb)];
    }
    
    [self.tableView reloadData];
}

#pragma mark  -- ScenicDetailIntroduceDelegate

- (void)goAction:(NSInteger)type {
    
    switch (type) {
        case MerchantTypeTel:
        {
            [self phoneAction];
        }
            break;
        case MerchantTypeWeb:
        {
            [self websiteAction];
        }
            break;
        case MerchantTypeAdress:
        {
            [self navAction];
        }
            break;
        default:
            break;
    }
}

- (void)navAction{
    
    CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(self.model.lat.doubleValue, self.model.lng.doubleValue);
    [IGUtil tractficToLocation:transformFromWGSToGCJ(coordinate, nil) vc:self];
}

- (void)phoneAction{
    
    NSString * phoneNum = [[self.model.tel stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSString * str = [NSString stringWithFormat:@"tel://%@",phoneNum];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}
- (void)websiteAction{
    
    NSString * str = [self.model.website stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSURL *url= [NSURL URLWithString:str];
    [[UIApplication sharedApplication] openURL:url];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat offeset = scrollView.contentOffset.y;
    CGFloat height = kScreenWidth * 9 / 16;
    
    self.barBGAlpha = offeset / height;
    
    if (self.barBGAlpha > 0.8) {
        self.customBarStyle = UIStatusBarStyleDefault;
    } else {
        self.customBarStyle = UIStatusBarStyleLightContent;
    }
    
    if (offeset <= 0) {
        
        [self.headScrollView setOffSetY:offeset];
    } else {
        
        [self.headScrollView setOffSetY:0];
    }
}


#pragma mark - Table view data source


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    if (!self.model) {
        return 0;
    }
    
//    NSInteger count = 1;
//
//    if (self.model.promotion.count > 0) {
//        count += 1;
//    }
//
//    if(self.tagArray.count > 0){
//        count += 1;
//    }
//
//    if (self.model.business.count > 0) {
//        count += 1;
//    }
    
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
        NSInteger n = 0;
        if (self.model.smartName.length != 0) {
            n+=1;
        }
        if(self.model.smartIntro.length != 0){
             n+=1;
        }
        return n;//
    }else if(section == 1) {

        if (self.model.promotion.count > 0) {
            
            return self.model.promotion.count;
        }
        return self.model.business.count;
    }else if (section == 2) {
       return  self.tagArray.count;
        
    }else if(section == 3) {
        
        return self.model.business.count;
    }
    
    return 0;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        
        if((self.model.smartIntro.length != 0 && indexPath.row == 1) || self.model.smartName.length == 0){
            MerchantDescCell * cell = [tableView dequeueReusableCellWithIdentifier:@"MerchantDescCell"];
            
            cell.descLabel.text = self.model.smartIntro;
            return cell;
        }else if (self.model.smartName.length != 0 ) {
            MerchantIntroduce * cell = [tableView dequeueReusableCellWithIdentifier:@"MerchantIntroduce"];
            cell.nameLabel.text = self.model.smartName;
            cell.tagView.tagsArray = self.model.tagsArray;
            cell.playpath = self.model.playpath;
            
            return cell;
        }
    }else if(indexPath.section == 1){
        
        if (self.model.promotion.count > 0) {
            MerchantEventModel *model = self.model.promotion[indexPath.row];
            
            MerchantEventCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MerchantEventCell"];
            
            cell.model = model;
            
            return cell;
        }
    }else  if (indexPath.section == 2) {
        
        NSNumber * type = self.tagArray[indexPath.row];
        
        switch (type.integerValue) {
                
            case MerchantTypeAdress:
            {
                ScenicDetailIntroduce * cell = [tableView dequeueReusableCellWithIdentifier:@"ScenicDetailIntroduce"];
                cell.delegate = self;
                
                [cell setDataWith:[NSString stringWithFormat:@"%@:",MYLocalizedString(@"SITELIST_ADDRESS", nil)] Address:self.model.address];
                return cell;
            }
                break;
            case MerchantTypeOpenTime:
            case MerchantTypeTel:
            case MerchantTypeWeb:
            {
                MerchantInforCell * cell = [tableView dequeueReusableCellWithIdentifier:@"MerchantInforCell"];
                
                cell.delegate = self;
                cell.type = type.integerValue;
                cell.model = self.model;
                
                return cell;
            }
                break;
            default:
                break;
        }
    }
    

    RecommendMerchantCell * cell = [tableView dequeueReusableCellWithIdentifier:@"RecommendMerchantCell"];
    
    MerchantModel *model = self.model.business[indexPath.row];
    
    cell.superVC = self;
    cell.model = model;
    
    
    UIView *lineView = [cell viewWithTag:1000];
    
    if (!lineView) {
        lineView = [UIView new];
        lineView.backgroundColor = kLineColor;
        lineView.tag = 1000;
        
        [cell addSubview:lineView];
        
        @weakify(cell);
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            @strongify(cell);
            
            make.left.equalTo(cell.mas_left).offset(16);
            make.right.equalTo(cell.mas_right).offset(-16);
            make.top.equalTo(cell.mas_top);
            make.height.mas_equalTo(1);
        }];
    }
    if (indexPath.row == 0) {
        lineView.hidden = YES;
    } else {
        
        lineView.hidden = NO;
    }
    
    return cell;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    if ((section == 1 && self.model.promotion.count > 0) || (section == 3 && self.model.business.count > 0)) {
        
        
        SectionTitleHeaderView * headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"SectionTitleHeaderView"];
        headerView.contentView.backgroundColor = [UIColor whiteColor];
        headerView.titleLabel.font = FontBold(14);
        headerView.titleLabel.textColor = kSubitleColor;
        
        if (self.model.promotion.count > 0 && section == 1) {
            
            headerView.titleLabel.text = MYLocalizedString(@"HOMEPAGE_EVENT", nil);
        } else {
            
            headerView.titleLabel.text = MYLocalizedString(@"HOMEPAGE_NEAR_BUSINESS", nil);
        }
        
        
        return headerView;
    }

    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    if ([cell isKindOfClass:[MerchantEventCell class]]) {
        
        MerchantEventModel *model = self.model.promotion[indexPath.row];
        
        NSString *url = model.url;
        
        if (url.length > 0) {
            
            WebViewVC *webVC = [WebViewVC new];
            webVC.url = url;
            
            [self.navigationController pushViewController:webVC animated:YES];
        }
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    UITableViewHeaderFooterView * footerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"UITableViewHeaderFooterView"];
    
    if (!footerView) {
        footerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:@"UITableViewHeaderFooterView"];
        
        footerView.contentView.backgroundColor = kBGColor;
    }
    
    return footerView;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
   if ((section == 1 && self.model.promotion.count > 0) || (section == 3 && self.model.business.count > 0)) {
        
        return 40.f;
    }
    return 0.0001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 0) {
        return 0.000001;
    }else if (section == 1 && self.model.promotion.count == 0) {
        return 0.000001;
    }else if(section == 2 && self.model.tagsArray.count == 0){
        return 0.00001;
    }
    return 10.f;
}

@end
